<?php

include_once ('bn_path.php');

class bn_menu
{

    private $dl;
    private $mnu;
    private $gf;
    public $css_ul = '';
    public $css_li = '';
    public $css_a = '';
    public $subli_show = '>>';
    protected static $instance;

    public function __construct()
    {
        $this->dl = new bn_dal;
        $this->mnu = new bn_menuctrl;
        $this->gf=new bn_genralfunc;
    }

    public static function getInstance()
    {
        if (!isset(self::$instance))
        {
            self::$instance = new self();
        }
        return self::$instance;
    }

    public function insert($pid, $type, $name, $url, $descr)
    {
        return $this->dl->insert('menus', func_get_args());
    }

    public function update($name, $url, $descr, $id)
    {
        return $this->dl->update('menus', array(
            'name' => $name,
            'url' => $url,
            'descr' => $descr), array('id=' => $id));
    }

    public function delete($id)
    {
        $this->dl->query('delete from menus where pid=?', $id);
        return $this->dl->delete('menus', $id);
    }
    public function select_all($type = '')
    {
        if (!empty($type))
            $type = array('type=' => $type);
        if (strpos('user', $type) !== false)
            return $this->dl->select('select id,pid,name,concat("' . bn_Get_sitename() . '/' .
                root . '",url)url,descr from menus', $type);
        else
            return $this->dl->select('select id,pid,name,url,descr from menus', $type);
    }

    public function has_menu($type, $url)
    {
        return $this->dl->hasrow('select id from menus where type=? and url=?', array($type,
                $url));
    }

    public function select_menuPID($pid, $type)
    {
        return $this->dl->select('select id,name,url from menus where pid=? and type=?',
            array($pid, $type));
    }

    public function treemenu($type)
    {
        if (!empty($type))
        {
            $this->mnu->css_link = $this->css_a;
            $this->mnu->css_item = $this->css_li;
            $this->mnu->css_class = $this->css_ul;
            $this->mnu->textCOL = 'name';
            return $this->mnu->treemenu(self::select_all($type)->fetch());
        }
    }

    public function menu($type)
    {
        if (!empty($type))
        {
            $this->mnu->css_link = $this->css_a;
            $this->mnu->css_item = $this->css_li;
            $this->mnu->css_class = $this->css_ul;
            $this->mnu->textCOL = 'name';
            return $this->mnu->navmenu(self::select_all($type)->fetch());
        }
    }

    public function ddl_menutype($args=array('name' => 'menuddl', 'cssclass' => 'select'))
    {
        $arr = array('user1','user2','user3','user4','user5');
        $ddl = new bn_DropDownList();
        $this->gf->class_property($ddl,$args);
        return $ddl->show($arr);
    }

    public function __destruct()
    {
        unset($this->dl);
        unset($this->mnu);
        unset($this->gf);
    }
}

?>